package com.whz.gateway.configure.access.rules.black;

import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import com.whz.gateway.common.GatewayMessageAccessor;
import com.whz.gateway.common.constants.GatewayConstant;
import com.whz.gateway.common.util.PathMatcherUtils;
import com.whz.gateway.limit.IBlackRule;

import java.util.Arrays;

/**
 * private 路径禁止外网访问,这样的url都不走gateway
 *
 * @author whz
 */
@Component
public class UrlForbiddenRule implements IBlackRule {
    @Override
    public String getName() {
        return "UrlForbiddenRule";
    }

    @Override
    public String getMessage() {
        return GatewayMessageAccessor.getMessage("gateway.config.rules.black.UrlForbiddenRule.URL_FORBIDDEN");
    }

    @Override
    public boolean test(ServerWebExchange exchange) {
        return Arrays.stream(GatewayConstant.BLACK_LIST_PATHS).anyMatch(path -> PathMatcherUtils.match(path, exchange.getRequest().getPath().value()));
    }
}